package com.shyroke.daydayzhuansvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.shyroke.daydayzhuanapi.dto.SubmitArt;
import com.shyroke.daydayzhuanapi.dto.SubmitArtAdminDto;
import com.shyroke.daydayzhuanapi.dto.SubmitArtDto;
import com.shyroke.daydayzhuanapi.entity.QSubmitArtEntity;
import com.shyroke.daydayzhuanapi.entity.QUserEntity;
import com.shyroke.daydayzhuanapi.entity.SubmitArtEntity;
import com.shyroke.daydayzhuanapi.mapper.SubmitArtMapper;
import com.shyroke.daydayzhuansvc.service.SubmitArtService;
import com.shyroke.daydayzhuansvc.util.Page;
import com.shyroke.daydayzhuansvc.util.TabelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubmitArtServiceImpl implements SubmitArtService {

    @Autowired
    private JPAQueryFactory factory;
    @Autowired
    private SubmitArtMapper submitArtMapper;
    @Autowired
    private OSSService ossService;

    @Override
    public TabelData<SubmitArtDto> getProjectList(Page page, SubmitArt submitArt) {
        QSubmitArtEntity qSubmitArtEntity = QSubmitArtEntity.submitArtEntity;
        JPAQuery<SubmitArtEntity> query = factory.selectFrom(qSubmitArtEntity);

        if(submitArt != null && submitArt.getUserId() != null){
            query = query.where(qSubmitArtEntity.createId.eq(submitArt.getUserId()));
        }
        if(StrUtil.isNotBlank(submitArt.getTitle())){
            query = query.where(qSubmitArtEntity.title.like("%"+submitArt.getTitle()+"%"));
        }

        QueryResults<SubmitArtEntity> queryResults = query
                .offset(PageRequest.of(page.getPage() - 1, page.getLimit()).getOffset()).limit(page.getLimit())
                .orderBy(qSubmitArtEntity.id.desc())
                .fetchResults();

        List<SubmitArtDto> list = queryResults.getResults().stream().map(e -> submitArtMapper.entityToDto(e)).collect(Collectors.toList());

        return new TabelData(list,(int)queryResults.getTotal());
    }

    @Override
    public TabelData<SubmitArtAdminDto> getProjectListAdmin(Page page, SubmitArt submitArt) {
        QSubmitArtEntity qSubmitArtEntity = QSubmitArtEntity.submitArtEntity;
        QUserEntity qUserEntity = QUserEntity.userEntity;
        JPAQuery<SubmitArtEntity> query = factory.selectFrom(qSubmitArtEntity)
                .leftJoin(qUserEntity).on(qSubmitArtEntity.createId.eq(qUserEntity.id));

        if(submitArt != null && submitArt.getUserId() != null){
            query = query.where(qSubmitArtEntity.createId.eq(submitArt.getUserId()));
        }
        if(StrUtil.isNotBlank(submitArt.getTitle())){
            query = query.where(qSubmitArtEntity.title.like("%"+submitArt.getTitle()+"%"));
        }

        QueryResults<SubmitArtEntity> queryResults = query
                .offset(PageRequest.of(page.getPage() - 1, page.getLimit()).getOffset()).limit(page.getLimit())
                .orderBy(qSubmitArtEntity.id.desc())
                .fetchResults();

        List<SubmitArtAdminDto> list = queryResults.getResults().stream().map(e -> {
            SubmitArtAdminDto dto = submitArtMapper.entityToAdminDto(e);
            if(StrUtil.isNotBlank(dto.getPay())){
                dto.setPay(ossService.getImgUrl(dto.getPay()));
            }
            return dto;
        }).collect(Collectors.toList());

        return new TabelData(list,(int)queryResults.getTotal());
    }
}
